﻿using System;
using System.Collections.Generic;
using System.Text;
using Telerik.Web.UI;
using Acms.WebControls;
using System.Web.UI;
using Acms.Bll;

namespace Acms.AcmsControls.ContentControls.Admin
{
    public class FlashVideoAdminEdit : BaseAdminEdit<FlashVideoContentControlProperties>
    {
        RadUpload upload = new RadUpload();
        RadProgressManager manager = new RadProgressManager();
        RadProgressArea progress = new RadProgressArea();

        FlashControl flash = new FlashControl();

        public override void PropertiesLoaded(FlashVideoContentControlProperties properties)
        {
            if (properties != null)
            {
                HasFile = properties.HasResource();
                flash.Variables["videoUrl"] = Page.ResolveUrl(RenderFile.GetUrl(Properties.ResourceId, Page));
            }
            else
            {
                HasFile = false;
            }

            base.PropertiesLoaded(properties);
        }
        protected override void SaveToProperties(FlashVideoContentControlProperties properties, Acms.Bll.PostControl control)
        {
            if (upload.UploadedFiles.Count == 1)
            {
                File file = File.CreateInstance(File.FileTypes.FlashVideo, upload.UploadedFiles[0].GetNameWithoutExtension(), upload.UploadedFiles[0].ContentType, upload.UploadedFiles[0].ContentLength, upload.UploadedFiles[0].InputStream);

                file.BeginTransaction();
                control.UseTransaction(file.TransactionKey);

                file.Update();

                properties.ResourceId = file.Id;
            }

        }

        protected override string GetCssClass
        {
            get { return "flashVideo"; }
        }

        private bool HasFile{get;set;}
        protected override object SaveViewState()
        {
            ViewState["HasFile"] = HasFile;
            return base.SaveViewState();
        }

        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);
            HasFile = (bool)ViewState["HasFile"];
        }

        protected override void CreateChildControls()
        {
            upload.ID = "upload";
            upload.ControlObjectsVisibility = ControlObjectsVisibility.None;
            this.Controls.Add(upload);
            manager.ID = "manager";
            this.Controls.Add(manager);
            progress.ID = "progress";
            this.Controls.Add(progress);


            flash.WMode = Acms.WebControls.FlashControl.WModes.Transparent;
            flash.FlashFileUrl = Page.ResolveUrl("~/Flash/FlashVideo.swf");
            flash.Variables["videoSkinUrlStr"] = Page.ResolveUrl("~/Flash/custom.swf");
            flash.Width = flash.Height = 500;
            this.Controls.Add(flash);

            base.CreateChildControls();
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            upload.RenderControl(writer);

            if (HasFile)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Class, "form");
                writer.RenderBeginTag(HtmlTextWriterTag.Ul);

                writer.RenderBeginTag(HtmlTextWriterTag.Li);
                writer.AddAttribute(HtmlTextWriterAttribute.For, flash.ClientID);
                writer.RenderBeginTag(HtmlTextWriterTag.Label);
                writer.Write("Existing Flash Video:");
                writer.RenderEndTag();//lable

                writer.RenderBeginTag(HtmlTextWriterTag.Span);
                flash.RenderControl(writer);
                writer.RenderEndTag();//span

                writer.RenderEndTag();//li


                writer.RenderEndTag();//ul
            }
        }
    }
}
